package com.party.core.service.mall.biz;

import com.party.common.utils.StringUtils;
import com.party.core.model.mall.MallGoodsDetail;
import com.party.core.service.mall.IMallGoodsDetailService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商品详情业务
 * Created by yifeng
 *
 * @date 2018/3/28 0028
 * @time 上午 10:56
 */
@Service
public class GoodsDetailBizService {

    @Autowired
    private IMallGoodsDetailService mallGoodsDetailService;

    /**
     * 转化成HTML
     *
     * @param goodsId 商品id
     * @return
     */
    public MallGoodsDetail castToHtml(String goodsId) {
        MallGoodsDetail goodsDetail = mallGoodsDetailService.findByGoodsId(goodsId);
        if (goodsDetail != null) {
            if (StringUtils.isNotEmpty(goodsDetail.getContent())) {
                goodsDetail.setContent(StringUtils.unescapeHtml4Video2Iframe(goodsDetail.getContent()));
            }
        }
        return goodsDetail;
    }

    /**
     * 保存详情
     *
     * @param goodsId     商品id
     * @param goodsDetail 详情
     */
    public void saveDetailBiz(String goodsId, MallGoodsDetail goodsDetail) {
        if (StringUtils.isNotEmpty(goodsId)) {
            MallGoodsDetail detailDB = mallGoodsDetailService.findByGoodsId(goodsId);
            if (detailDB == null) {
                detailDB = new MallGoodsDetail();
                detailDB.setGoodsId(goodsId);
            }
            if (StringUtils.isNotEmpty(goodsDetail.getContent())) {
                String content = StringUtils.escapeHtml4Iframe2Video(goodsDetail.getContent().trim());
                detailDB.setContent(content);
            }
            if (StringUtils.isNotEmpty(detailDB.getId())) {
                mallGoodsDetailService.update(detailDB);
            } else {
                mallGoodsDetailService.insert(detailDB);
            }
        }
    }

    /**
     * 复制详情
     *
     * @param oldGoodsId 旧的商品
     * @param newGoodsId 新的商品
     */
    public void copyDetail(String oldGoodsId, String newGoodsId) {
        MallGoodsDetail mallGoodsDetail = mallGoodsDetailService.findByGoodsId(oldGoodsId);
        mallGoodsDetail.setGoodsId(newGoodsId);
        mallGoodsDetail.setId(null);
        mallGoodsDetail.setCreateDate(null);
        mallGoodsDetail.setUpdateDate(null);
        mallGoodsDetailService.insert(mallGoodsDetail);
    }

    public void deleteByGoods(String goodsId) {
        mallGoodsDetailService.deleteByGoodsId(goodsId);
    }
}
